package com.ruoyi.dictionaries.service.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.dictionaries.Utils.BaiduMapUtil;
import com.ruoyi.dictionaries.pojo.BaiduMapVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dictionaries.mapper.RegionsMapper;
import com.ruoyi.dictionaries.domain.Regions;
import com.ruoyi.dictionaries.service.IRegionsService;

/**
 * 地区管理Service业务层处理
 *
 * @author gdslovehy
 * @date 2025-04-07
 */
@Slf4j
@Service
public class RegionsServiceImpl implements IRegionsService
{
    @Autowired
    private RegionsMapper regionsMapper;

    /**
     * 查询地区管理
     *
     * @param id 地区管理主键
     * @return 地区管理
     */
    @Override
    public Regions selectRegionsById(String id)
    {
        return regionsMapper.selectRegionsById(id);
    }

    /**
     * 查询地区管理列表
     *
     * @param regions 地区管理
     * @return 地区管理
     */
    @Override
    public List<Regions> selectRegionsList(Regions regions)
    {
        return regionsMapper.selectRegionsList(regions);
    }

    /**
     * 新增地区管理
     *
     * @param regions 地区管理
     * @return 结果
     */
    @Override
    public int insertRegions(Regions regions)
    {
        return regionsMapper.insertRegions(regions);
    }

    /**
     * 修改地区管理
     *
     * @param regions 地区管理
     * @return 结果
     */
    @Override
    public int updateRegions(Regions regions)
    {
        return regionsMapper.updateRegions(regions);
    }

    /**
     * 批量删除地区管理
     *
     * @param ids 需要删除的地区管理主键
     * @return 结果
     */
    @Override
    public int deleteRegionsByIds(Long[] ids)
    {
        return regionsMapper.deleteRegionsByIds(ids);
    }

    /**
     * 删除地区管理信息
     *
     * @param id 地区管理主键
     * @return 结果
     */
    @Override
    public int deleteRegionsById(Long id)
    {
        return regionsMapper.deleteRegionsById(id);
    }

    @Override
    public List<Regions> selectRegionsListByParentId(Long parentId) {
        return regionsMapper.selectRegionsListByParentId(parentId);
    }

    @Override
    public int deleteAllRegions() {
        return regionsMapper.deleteAllRegions();
    }

    @Override
    public Boolean syncArea() {
        Map<String, String> params = new LinkedHashMap<>();
        params.put("keyword", "中国");
        params.put("sub_admin", "3");
        params.put("extensions_code", "1");
        try {
            List<BaiduMapVO> baiduMapVOList = BaiduMapUtil.requestGetSN(params);
            fillArea(baiduMapVOList);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("同步异常:{}", e.getMessage());
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @Override
    public void fillArea(List<BaiduMapVO> baiduMapVOList) {
        regionsMapper.deleteAllRegions();
        for (BaiduMapVO baiduMapVO : baiduMapVOList) {
            saveArea(null, baiduMapVO);
        }
    }

    @Override
    public void saveArea(String parentId, BaiduMapVO baiduMapVO) {
        Regions area = new Regions();
        String areaId = baiduMapVO.getCode();
        if(areaId == null || areaId.equals("0")){
            areaId = "1";
            parentId = "0";
        }
        if(!areaId.equals("1") && parentId.equals("0")){
            parentId = "1";
        }
        area.setId(areaId);
        area.setName(baiduMapVO.getName());
        area.setLevel(Convert.toInt(baiduMapVO.getLevel()));
        area.setParentId(Long.valueOf(parentId));
        //todo 落库逻辑
        insertArea(area);
        List<BaiduMapVO> districts = baiduMapVO.getDistricts();
        if (CollUtil.isNotEmpty(districts)) {
            for (BaiduMapVO mapVO : districts) {
                saveArea(areaId, mapVO);
            }
        }
    }

    @Override
    public void insertArea(Regions area) {
        //todo 落库逻辑
        regionsMapper.insertRegions(area);
    }
}
